<?php include('includes/connection_settings.php');

if (!isset($_COOKIE['username'])) {
 echo("Log in first!");
 exit;
}

//Strips " and ' marks
$bannedMarks = array("\"", "'");

$_COOKIE['username'] = str_replace($bannedMarks, "", $_COOKIE['username']);


//
// CREATES HIDDEN INPUT FOR STARTING LANGUAGE AND VARIABLE $STARTINGLANGUAGE
//
 $table = mysql_query("SELECT startingLanguage FROM users WHERE username = '{$_COOKIE['username']}'") or die('An error in the command.');
 $startingLanguage = mysql_result($table, 0, 0);

 if ($startingLanguage == "mix") {
  $oneOrTwo = rand(1,2);
  if ($oneOrTwo == 1) {
   $startingLanguage = "local";
  }
  else {
   $startingLanguage = "theForeign";
  }
 }

 else {
  if ($startingLanguage == "foreign") {
   $startingLanguage = "theForeign";
  }
 }
/////


//
// PICKS MINIMUM POINTS OF THE CHOOSEN DICTIONARYS
//
 $table = mysql_query("SELECT dictionary FROM choices WHERE {$_COOKIE['username']} = true");
 if (!isset($table)) {
  echo "<h1>Can't choose data from the table!</h1><p>" . mysql_error($connection) . "</p>";
  exit;
 }

 if (mysql_result($table, 0, 0) == "") {
  echo "<p>No dictionaries selected! Please, <span class='likeLink' onClick='showSwichDictionaries()'>choose your dictionaries</span></p></form>";
  exit;
 }

 for ($i = 0;$i < mysql_num_rows($table); $i++) {
  $dictionariesExplodedToArray[$i] = mysql_result($table, $i, 0);
 }

 for ($i = 0; $i <= count($dictionariesExplodedToArray) - 1; $i++) {
  if ($i == 0) {
   $dictionaryPartOfQueryInMinimumPointsSection = "dictionary = '{$dictionariesExplodedToArray[0]}'";
  }
  else {
   $dictionaryPartOfQueryInMinimumPointsSection = $dictionaryPartOfQueryInMinimumPointsSection . " OR dictionary = '{$dictionariesExplodedToArray[$i]}'";
  }
 }

 $table = mysql_query("SELECT MIN(points_{$_COOKIE['username']}) FROM words WHERE {$dictionaryPartOfQueryInMinimumPointsSection}");
 if (!isset($table)) {
  echo "<h1>Can't choose data from the table!</h1><p>" . mysql_error($connection) . "</p>";
  exit;
 }
 $minimumPoints = mysql_result($table, 0, 0);
/////



//
// CREATES A TABLE WHERE IS EVERY WORD PAIR FROM CHOOSEN DICTIONARYS WITH MINIMUM POINTS
//
 for ($i = 0; $i <= count($dictionariesExplodedToArray) - 1; $i++) {
  if ($i == 0) {
   $endOfQueryInWordCollectionSection = "dictionary = '{$dictionariesExplodedToArray[0]}'";
  }
  else {
   $endOfQueryInWordCollectionSection = $endOfQueryInWordCollectionSection . " OR points_{$_COOKIE['username']} = {$minimumPoints} AND dictionary = '{$dictionariesExplodedToArray[$i]}'";
  }
 }

 $table = mysql_query("SELECT * FROM words WHERE points_{$_COOKIE['username']} = {$minimumPoints} AND {$endOfQueryInWordCollectionSection}");
 if (!isset($table)) {
  echo "<h1>Can't choose data from the table!</h1><p>" . mysql_error($connection) . "</p>";
  exit;
 }
/////



//
// PICKS A RANDOM WORD PAIR FROM THE TABLE (LOOK PREVIOUS SECTION) AND ECHOS LOCAL OR FOREIGN WORD (AND OTHER USEFULL STUFF)
//
 $lastWordPairsNumber = mysql_num_rows($table) - 1;
 $numberOfWordPair = rand(0, $lastWordPairsNumber);

 echo "<h1>Query</h1>";

 echo "<p>" . mysql_result($table, $numberOfWordPair, $startingLanguage); // !!!

 echo " = <input type='text' id='theForeign' maxlength='30' />";
 echo "<input type='button'
 onClick='checkWordAppearance(\"{$startingLanguage}\", \"$minimumPoints\", \"{$numberOfWordPair}\", document.getElementById(\"theForeign\").value)'
 value='Check it!' /></p>";
/////

?>
